package cst.a931xmc.yuejian20.Model;

import java.util.List;

import cst.a931xmc.yuejian20.Bean.ClubBean;
import cst.a931xmc.yuejian20.Bean.VideoBean;
import cst.a931xmc.yuejian20.Interface.ItemIface;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Service.ClubService;
import cst.a931xmc.yuejian20.Service.VideoService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ClubModel<T> extends ApiModel implements ItemIface {
    private Retrofit retrofit;
    private ClubService service;
    private VideoService service1;

    public ClubModel(){
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(ClubService.class);
        service1 = retrofit.create(VideoService.class);
    }

    @Override
    public void getList(final Listener l) {
        Call<List<ClubBean>> call = service.getClubList();
        call.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getDetail(int club_id, final Listener l) {
        Call<ClubBean> call = service.getClubDetail(club_id);
        call.enqueue(new Callback<ClubBean>() {
            @Override
            public void onResponse(Call<ClubBean> call, Response<ClubBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<ClubBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void getVideoList(int club_id, final Listener l) {
        Call<List<VideoBean>> call = service1.getVideoList(club_id);
        call.enqueue(new Callback<List<VideoBean>>() {
            @Override
            public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    public void getVideo(int video_id, final Listener l) {
        Call<VideoBean> call = service1.getVideo(video_id);
        call.enqueue(new Callback<VideoBean>() {
            @Override
            public void onResponse(Call<VideoBean> call, Response<VideoBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<VideoBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }
}
